package com.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.model.Adminuser;
import com.zjitc.model.Category;
import com.zjitc.model.CategorySecond;
import com.zjitc.model.Orders;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.AdminService;
import com.zjitc.service.OrderService;
import com.zjitc.service.ProductService;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/28 16:15
 * @description:
 */
@Controller
public class Admin {

  @Autowired
  AdminService adminService;

  @Autowired
  ProductService productService;

  @Autowired
  OrderService orderService;

  /*用户登录**/
  @RequestMapping("/adminLogin")
  public String adminLogin(HttpSession session) {
    return "/admin/login.jsp";
  }

  @RequestMapping("/adminDoLogin")
  public String admindoLogin(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      HttpSession session) {
    Adminuser adminuser = adminService.doLogin(username, password);
    if (adminuser == null) {
      session.setAttribute("msg", "用户名或密码错误！");
      return "/admin/login.jsp";
    }
    session.setAttribute("adminuser", adminuser);
    return "/admin/home.jsp";
  }

  @RequestMapping("/adminLogout")
  public String adminLogout(HttpSession session) {
    session.removeAttribute("adminuser");
    return "/admin/top.jsp";
  }

  /*用户管理*/
  @RequestMapping("/findAllUser")
  public String findAllUser(User user, HttpSession session) {
    List<User> list = adminService.findAllUser();
    session.setAttribute("list", list);
    return "/admin/user/list.jsp";
  }

  @RequestMapping("/userDelete.do")
  public String userDelete(Integer uid) {
    adminService.userDelete(uid);
    return "forward:findAllUser.do";
  }

  /*一级列表管理*/
  @RequestMapping("/findAllCategory")
  public String findAllCategory(HttpSession session) {
    List<Category> list = adminService.findAllCategory();
    session.setAttribute("list", list);
    return "/admin/category/list.jsp";
  }

  @RequestMapping("/categoryAddUI")
  public String addUI() {
    return "/admin/category/add.jsp";
  }

  @RequestMapping("/categoryAdd")
  public String categoryAdd(Category category) {
    adminService.add(category);
    return "forward:findAllCategory.do";
  }

  @RequestMapping("categoryDelete")
  public String categoryDelete(Integer cid) {
    adminService.delete(cid);
    return "forward:findAllCategory.do";
  }

  @RequestMapping("categoryEditUI")
  public String categoryEditUI(HttpSession session, Integer cid) {
    Category category = adminService.findByCid(cid);
    session.setAttribute("category", category);
    return "/admin/category/edit.jsp";
  }

  @RequestMapping("categoryEdit")
  public String categoryEdit(Category category) {
    adminService.update(category);
    return "forward:findAllCategory.do";
  }

  /*二级分类管理**/

  @RequestMapping("/findAllCategorySecond")
  public String findAllCS(HttpSession session) {
    List<CategorySecond> list = adminService.findAllCS();
    session.setAttribute("list", list);
    return "/admin/categorysecond/list.jsp";
  }

  @RequestMapping("/CategorySecondAddUI")
  public String CategorySecondAddUI(HttpSession session) {
    List<Category> list = adminService.findAllCategory();
    session.setAttribute("list", list);
    return "/admin/categorysecond/add.jsp";
  }

  @RequestMapping("/CategorySecondAdd")
  public String CategorySecondAdd(CategorySecond categorySecond) {
    adminService.csAdd(categorySecond);
    return "forward:findAllCategorySecond.do";
  }

  @RequestMapping("/CategorySecondDelete")
  public String CategorySecondDelete(Integer csid) {
    adminService.csDelete(csid);
    return "forward:findAllCategorySecond.do";
  }

  @RequestMapping("/CategorySecondEditUI")
  public String CategorySecondEditUI(HttpSession session, Integer csid) {
    CategorySecond categorySecond = adminService.findByCsid(csid);
    session.setAttribute("categorySecond", categorySecond);
    return "/admin/categorysecond/edit.jsp";
  }

  @RequestMapping("/CategorySecondEdit")
  public String CategorySecondEdit(CategorySecond categorySecond) {
    adminService.csUpdate(categorySecond);
    return "forward:findAllCategorySecond.do";
  }

  /*商品管理*/
  @RequestMapping("/findAllProduct")
  public String findAllProduct(
      @RequestParam(name = "start", required = false, defaultValue = "1") int start,
      @RequestParam(name = "count", required = false, defaultValue = "12") int count,
      HttpSession session) {
    PageHelper.startPage(start, count);
    List<Product> list = productService.findAllProductByPage();
    PageInfo<Product> pageInfo = new PageInfo<>(list);
    session.setAttribute("page", pageInfo);
    return "/admin/product/list.jsp";
  }

  @RequestMapping("/productAddUI")
  public String productAddUI(HttpSession session) {
    List<CategorySecond> list = adminService.findAllCS();
    session.setAttribute("list", list);
    return "/admin/product/add.jsp";
  }


  @RequestMapping("/AdminProductAdd")
  public String AdminProductAdd(
      HttpSession session,
      @RequestParam(name = "pname") String pname,
      @RequestParam(name = "is_Hot") int is_Hot,
      @RequestParam(name = "shop_Price") double shop_Price,
      @RequestParam(name = "market_Price") double market_Price,
      @RequestParam(name = "csid") Integer csid,
      @RequestParam(name = "pdesc") String pdesc,
      @RequestParam(name = "upload") MultipartFile image
  ) throws IOException {
    Product product = new Product();
    product.setPname(pname);
    product.setIs_hot(is_Hot);
    product.setMarket_price(market_Price);
    product.setShop_price(shop_Price);
    product.setPdesc(pdesc);
    product.setCsid(csid);
    if (!image.isEmpty()){
      String path=session.getServletContext().getRealPath("/products/5");
      String filename=image.getOriginalFilename();
      File filepath=new File(path,filename);
      if (!filepath.getParentFile().exists()){
        filepath.getParentFile().mkdirs();
      }
      image.transferTo(new File(path+File.separator+filename));
      product.setImage("products/5/"+filename);
    }

    product.setPdate(new Date());
    adminService.pAdd(product);
    return "redirect:findAllProduct.do";
  }

  @RequestMapping("/productDelete")
  public String productDelete(Integer pid) {
    adminService.pDelete(pid);
    return "forward:findAllProduct.do";
  }

  @RequestMapping("/productEditUI")
  public String productEditUI(HttpSession session, Integer pid) {
    Product product = productService.findProductById(pid);
    List<CategorySecond> list = adminService.findAllCS();
    session.setAttribute("p", product);
    session.setAttribute("list", list);
    return "/admin/product/edit.jsp";
  }


  @RequestMapping("/productEdit")
  public String productEdit(
      @RequestParam(name = "pname") String pname,
      @RequestParam(name = "is_Hot") int is_Hot,
      @RequestParam(name = "shop_Price") double shop_Price,
      @RequestParam(name = "market_Price") double market_Price,
      @RequestParam(name = "csid") Integer csid,
      @RequestParam(name = "pdesc") String pdesc,
      @RequestParam(name = "upload") MultipartFile image,
      HttpSession session,
      Integer pid
  ) throws IOException {
    Product product = new Product();
    product.setPid(pid);
    product.setPname(pname);
    product.setIs_hot(is_Hot);
    product.setMarket_price(market_Price);
    product.setShop_price(shop_Price);
    product.setPdesc(pdesc);
    product.setCsid(csid);
    product.setPdate(new Date());
    if (!image.isEmpty()){
      String path=session.getServletContext().getRealPath("/products/5");
      String filename=image.getOriginalFilename();
      File filepath=new File(path,filename);
      if (!filepath.getParentFile().exists()){
        filepath.getParentFile().mkdirs();
      }
      image.transferTo(new File(path+File.separator+filename));
      product.setImage("products/5/"+filename);
    }
    adminService.pUpdate(product);
    return "forward:findAllProduct.do";
  }

  /*订单管理*/
  @RequestMapping("/orderList")
  public String orderList(
      @RequestParam(name = "start", defaultValue = "1") int start,
      @RequestParam(name = "start", defaultValue = "12") int count,
      HttpSession session
  ) {
    PageHelper.startPage(start, count);
    List<Orders> list = orderService.findPageAllOrders();
    PageInfo<Orders> info = new PageInfo<>(list);
    session.setAttribute("orders", list);
    session.setAttribute("page", info);
    return "/admin/order/list.jsp";

  }

  @RequestMapping("/updateOrderState")
  public String orderUpdate(
      String oid
  ) {
    orderService.updateOrderState(oid);
    return "redirect:orderList.do";
  }
}
